<template>
  <el-dialog :title="title" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('systemModule.role.table.roleName')" prop="name">
        <el-input v-model="formValidate.name" clearable :placeholder="$t('common.input')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('systemModule.role.table.remark')">
        <el-input
          v-model="formValidate.description"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('common.input')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-row>
        <el-tree
          ref="tree"
          class="admin-tree"
          :data="treeData"
          show-checkbox
          default-expand-all
          node-key="id"
          :default-checked-keys="formValidate.menuIds"
          highlight-current
          :props="defaultProps"
        />
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.save') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateRoles, addRoles } from '@/api/system'
export default {
  name: 'RoleSetting',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: {
      type: String,
      default: ''
    },
    treeData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      nextLoading: false,
      roleButtonData: null,
      formValidate: {
        name: null,
        id: null,
        description: null,
        menuIds: []
      },
      rules: {
        name: [{ required: true, message: this.$t('systemModule.role.table.roleName') + this.$t('common.null'), trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
      this.$refs.tree.setCheckedKeys([])
    },
    handleSubmit() {
      const key = this.$refs.tree.getCheckedKeys()
      if (!key || key.length === 0) {
        this.$message.error(this.$t('systemModule.role.tip'))
      }
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          this.submitLoading = true
          const postObj = {
            name: data['name'],
            description: data['description'],
            menuIds: key
          }
          if (data.id) postObj['id'] = data.id
          if (data.id) {
            updateRoles(postObj)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addRoles(postObj)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
